package com.demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/ViewCartInfo")
public class ViewCartInfo extends HttpServlet {

	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	
			throws ServletException, IOException {
	
		
		      HttpSession session=req.getSession();
		    Map<Integer, Integer> cartInfoMap=(Map<Integer, Integer>) session.getAttribute("cartInfo");
            System.out.println(cartInfoMap);
		    List<ProductDetails> productList=(List<ProductDetails>) session.getAttribute("productList");
            
		    
		    List<ProductDetails> selectedProductDetails=new  ArrayList<ProductDetails>();
		    
		    if(null!=cartInfoMap && cartInfoMap.size()>0){
		    	
		    	for (Integer productId : cartInfoMap.keySet()) {
		    		
		    		  for (ProductDetails productDetails : productList) {
		    			  
		    			      if(productId.equals(productDetails.getProductId())){

		    			    	  ProductDetails productDetails2=new ProductDetails();
		    			    	  productDetails2.setQuantity(cartInfoMap.get(productId));
		    			    	  productDetails2.setProductId(productId);
		    			    	  
		    			    	  productDetails2.setProductName(productDetails.getProductName());
		    			    	  
		    			    	  productDetails2.setPrice(productDetails.getPrice()*cartInfoMap.get(productId));
		    			    	  selectedProductDetails.add(productDetails2);
		    			    	    break;
		    			      }
					}
				}

		    }else {
		    	session.setAttribute("selectedProductList", "");
		    }
		    session.setAttribute("selectedProductList",selectedProductDetails );
		    resp.sendRedirect("viewCart.jsp");
		     
		     
		    
		    
	}
}
